/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controler;

import algorithm.Dijkstra;
import graph.DijkstraResult;
import graph.Node;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import view.NextPrevPanel;

/**
 *
 * @author borysfan
 */
public class PrevNextControler {
    private Controler controler;
    private NextPrevPanel panel;

    /*parametry potrzebne przy wyszukiwaniu*/
    private Node end;
    private boolean weatherCond;
    private boolean roadCond;
    private boolean trafficCond;
    private int criterium;
    private Node current;
    private ArrayList<Node> visited = new ArrayList<Node>();

    public PrevNextControler(Controler c){
        panel = new NextPrevPanel();
        panel.setVisible(false);
        controler=c;

        initActions();
    }

    public void initActions(){
        panel.addPrevBtnAction(new PrevAction());
        panel.addNextBtnAction(new NextAction());
        panel.addCloseBtnAction(new CloseAction());
    }

    public NextPrevPanel getView(){
        return panel;
    }

    public void setView(NextPrevPanel view){
        panel = view;
    }

    public void setStart(Node n){
        current = n;
        visited.add(n);
    }

    public void setEnd(Node n){
        end = n;
    }

    public void setWeatherCond(boolean value){
        weatherCond = value;
    }

    public void setRoadCond(boolean value){
        roadCond = value;
    }

    public void setTrafficCond(boolean value){
        trafficCond = value;
    }

    public void setCriterium(int value){
        criterium = value;
    }



    private class CloseAction implements ActionListener{

        public void actionPerformed(ActionEvent e) {
            controler.resetColor();
            panel.setVisible(false);
            
            controler.resetColor();
            controler.getMainPanel().getView().repaint();
            ArrayList<EdgeItemControler> edgesList = controler.getMainPanel().getEdgesList();
            for(int i=0;i<edgesList.size();i++){
                System.out.println(edgesList.get(i).getModel().getColor());
                edgesList.get(i).getView().updateUI();
            }

        }

    }

    private class NextAction implements ActionListener{

        public void actionPerformed(ActionEvent e) {
            System.out.println("prev");
            Dijkstra dijkstra = new Dijkstra();
            DijkstraResult resultDijkstra = null;
            current = visited.get(visited.size()-1);
            if(current.equals(end)==false){

            switch(criterium){
                case 0:
                        //dijkstra.computeByDistance(start,weatherCond,roadCond,trafficCond);
                        dijkstra.computeByDistance(current,  weatherCond, roadCond, trafficCond);


                case 1: //dijkstra.computeByTime(start,weatherCond,roadCond,trafficCond);
                        dijkstra.computeByTime(current,  weatherCond, roadCond, trafficCond);
                        break;

                case 2: //dijkstra.computeBySpeed(start,weatherCond,roadCond,trafficCond);
                        dijkstra.computeBySpeed(current, weatherCond, roadCond, trafficCond);
                        break;

            }
            resultDijkstra = dijkstra.getPath(current, end);
            if(resultDijkstra!=null){
                DijkstraResult res = resultDijkstra;
                if(res.getNodeList().size()>1){
                    visited.add(res.getNodeList().get(1));
                  
                }
             
                DijkstraResult tmp = new DijkstraResult();
                for(int i=0;i<visited.size();i++){
                    tmp.addNode(visited.get(i));
                }
                tmp.changeColor(Color.green );

                controler.resetColor();
                tmp.changeColor(Color.green);
                controler.getMainPanel().getView().repaint();
                ArrayList<EdgeItemControler> edgesList = controler.getMainPanel().getEdgesList();
                for(int i=0;i<edgesList.size();i++){
                    System.out.println(edgesList.get(i).getModel().getColor());
                    edgesList.get(i).getView().updateUI();
                }
                if(visited.get(visited.size()-1).equals(end)){
                    JOptionPane.showMessageDialog(controler.getMainWindow(), "Cel został osiągnięty!");
                }
            }
            }else{
                JOptionPane.showMessageDialog(controler.getMainWindow(), "Nie znaleziono żadnej trasy.");
            }

        }

    }

    private class PrevAction implements ActionListener{

        public void actionPerformed(ActionEvent e) {
            System.out.println("prev");
            Dijkstra dijkstra = new Dijkstra();
            ArrayList<DijkstraResult> resultDijkstra = null;
            if(visited.size()>1){
                visited.remove(visited.get(visited.size()-1));
            }
                  

           

            DijkstraResult tmp = new DijkstraResult();
            for(int i=0;i<visited.size();i++){
                tmp.addNode(visited.get(i));
            }
            tmp.changeColor(Color.green );

            controler.resetColor();
            tmp.changeColor(Color.green);
            controler.getMainPanel().getView().repaint();
            ArrayList<EdgeItemControler> edgesList = controler.getMainPanel().getEdgesList();
            for(int i=0;i<edgesList.size();i++){
                System.out.println(edgesList.get(i).getModel().getColor());
                edgesList.get(i).getView().updateUI();
            }
            

        }
    }

}
